package com.china08.yxyapi.api.manage.edu.platform.continuedu.lesson;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduLesson;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduLessonRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyContinueduLessonCrtController extends YxyApiBaseController {

    @Autowired
    YxyContinueduLessonRepository conLessonRepository;

    @ApiOperation(value = "")
    @RequestMapping(value = "/api_yw/manage/edu/platform/continuedu/lesson/crt1", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String name = reqModel.name;
        String summary = reqModel.summary;
        String cover = reqModel.cover;
        YxyProfile profile = getProfile(request, response);
        String createdBy = profile.getId();
        String schoolId = profile.getSchoolId();
        String author = profile.getUserNick();
        String areaCodeProvince = profile.getSchoolAreaIdProvince();
        String areaCodeCity = profile.getSchoolAreaIdCity();
        String areaCodeDistrict = profile.getSchoolAreaIdDistrict();

        YxyContinueduLesson continueduLesson = new YxyContinueduLesson();
        continueduLesson.setName(name);
        continueduLesson.setSummary(summary);
        continueduLesson.setCover(cover);
        continueduLesson.setCount(0);
        continueduLesson.setDuration(0);
        continueduLesson.setPv(0);
        continueduLesson.setVv(0);
        continueduLesson.setLastUpDate(new Date());
        // 草稿
        continueduLesson.setStatus(1);
        // 草稿
        continueduLesson.setIsVisible(0);
        continueduLesson.setSchoolId(schoolId);
        continueduLesson.setAreaCodeCity(areaCodeCity);
        continueduLesson.setAreaCodeDistrict(areaCodeDistrict);
        continueduLesson.setAreaCodeProvince(areaCodeProvince);
        continueduLesson.setAuthor(author);
        continueduLesson.setUserId(createdBy);
        continueduLesson = conLessonRepository.save(continueduLesson);

        RespModel respModel = new RespModel();
        respModel.id = continueduLesson.getId();
        return respModel;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String name;
        public String summary;
        public String cover;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(name), "请输入1-35个字的课程名称");
            Assert.isTrue(name.length() < 36 && name.length() > 0, "请输入1-35个字的课程名称");
            Assert.isTrue(StringUtils.isNotBlank(summary), "请输入1-500个字的课程简介");
            Assert.isTrue(summary.length() < 501 && summary.length() > 0, "请输入1-500个字的课程简介");
            Assert.isTrue(StringUtils.isNotBlank(cover), "请上传封面");
        }
    }

    public static class RespModel extends YxyApiRespModel {
        public String id;
    }
}
